package com.neu.yth.nepBackend.role.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neu.yth.nepBackend.entity.SysNode;
import com.neu.yth.nepBackend.enums.ResponseEnum;
import com.neu.yth.nepBackend.role.mapper.SysNodeMapper;
import com.neu.yth.nepBackend.role.service.ISysNodeService;
import com.neu.yth.nepBackend.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 系统功能节点树 服务实现类
 * </p>
 *
 * @author YTH
 * @since 2024-06-14
 */
@Service
public class SysNodeServiceImpl extends ServiceImpl<SysNodeMapper, SysNode> implements ISysNodeService {


    @Autowired
    private SysNodeMapper sysNodeMapper;
    /**
     * 查询所有节点树
     *
     * @return
     */
    @Override
    public Result selectAllTree() {
        LambdaQueryWrapper<SysNode> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.ne(SysNode::getState, 0);
        List<SysNode> sysnodes = sysNodeMapper.selectList(lambdaQueryWrapper);

        SysNode root =  list2Tree(sysnodes);
        if (root != null){
            return Result.success(root);
        }
        return Result.fail(ResponseEnum.RESULT_IS_EMPTY);

    }

    /**
     * 根据id查询节点
     *
     * @param roleId
     * @return
     */
    @Override
    public Result selectById(Integer roleId) {
        List<Integer> sysnodes = sysNodeMapper.selectByRoleId(roleId);
        if (sysnodes != null && sysnodes.size() > 0){
            return Result.success(sysnodes);
        }else {
            return Result.fail(ResponseEnum.RESULT_IS_EMPTY);
        }
    }

    /**
     * 根据id更新节点
     *
     * @param nodes
     * @param roleId
     * @return
     */
    @Override
    public Result updateNodeById(List<Integer>  nodes, Integer roleId) {
        if (nodes != null && nodes.size() > 0){
            sysNodeMapper.deleteByRoleId(roleId);
            sysNodeMapper.insertByRoleIds(roleId,nodes);
            return Result.success(null);
        }
        return null;
    }

    /**
     * 根据id查询菜单
     *
     * @param roleId
     * @return
     */
    @Override
    public Result selectMenuById(Integer roleId) {
        if (roleId != null){
            return Result.success(sysNodeMapper.selectMenuById(roleId));
        }
        return Result.fail(ResponseEnum.SELECT_FAILED);
    }

    private SysNode list2Tree(List<SysNode> sysnodes) {
//        1、寻找根节点
        SysNode root = null ;
        for (SysNode sysnode : sysnodes) {
            if(sysnode.getPid() == null){
                root =  sysnode;
                break;
            }
        }

//        填充根节点的孩子
        fillChildren( root , sysnodes );

        return   root;

    }

    private void fillChildren(SysNode root, List<SysNode> sysnodes) {
        if (root == null || sysnodes == null || sysnodes.size() == 0) {
            return;
        }
        if (root.getChildren() == null) {
            root.setChildren(new ArrayList<SysNode>());
        }

        for (SysNode sysnode : sysnodes) {
            if (sysnode.getPid() != null && sysnode.getPid().intValue() == root.getId().intValue()) {
                root.getChildren().add(sysnode);
            }
        }
//        递归填充孩子节点
        if (root.getChildren() == null ||  root.getChildren().size()== 0){
            return;
        }

        for (SysNode sysnode : root.getChildren()) {
            fillChildren(sysnode, sysnodes);
        }
    }
}
